package com.windo.animationview;

import android.graphics.Bitmap;
import android.util.Log;

public class Bubble {
	static int box_width;
	static int box_height;

	// Following properties are set in the constructor
	private int initial_x;
	private int initial_y;
	private int initial_xdelta;
	private int initial_ydelta;
	public int ball_width;
	public int radius;
	public Bitmap ball_pic;

	// Dynamic properties
	public int center_x; // The current position of the center of the ball
	public int center_y;
	public int pos_x; // The current position of the upper left corner of the
						// picture
	public int pos_y;
	public int delta_x; // The delta value of the ball(speed and direction)
	public int delta_y;
	private boolean _moveable;
	private String text;

	// Constructor
	Bubble(int initial_x_in, int initial_y_in, int initial_xdelta_in, int initial_ydelta_in, int ball_width_in, Bitmap ball_pic_in) {
		initial_x = initial_x_in;
		initial_y = initial_y_in;
		delta_x = initial_xdelta_in;
		delta_y = initial_ydelta_in;
		ball_width = ball_width_in;
		ball_pic = ball_pic_in;
		radius = ball_width / 2;
		center_x = initial_x + radius;
		center_y = initial_y + radius;
		pos_x = initial_x;
		pos_y = initial_y;
		_moveable = true;
	}

	public void move_ball() {
		if (!_moveable)
			return;
		pos_x += delta_x;
		pos_y += delta_y;

		if ((pos_x > (box_width - ball_width)) || (pos_x < 0)) {
			flip_horz_direction();
		}
		if ((pos_y > (box_height - ball_width)) || (pos_y < 0)) {
			flip_vert_direction();
		}

		center_x = pos_x + radius;
		center_y = pos_y + radius;

	}

	public void set_movable(boolean movable) {
		_moveable = movable;
	}

	public void dragBall(int x, int y) {
		pos_x = x - radius;
		pos_y = y - radius;
		if (pos_x <= 0)
			pos_x = 1;
		if (pos_x >= box_width - ball_width)
			pos_x = box_width - ball_width - 1;
		if (pos_y >= box_height - ball_width)
			pos_y = box_height - ball_width - 1;
		if (pos_y <= 0)
			pos_y = 1;

		center_x = pos_x + radius;
		center_y = pos_y + radius;
	}

	public boolean isInside(int x, int y) {
		Log.v("Bubble", "touch-x=" + x + " touch-y=" + y + " pos_x=" + pos_x + "pox_y=" + pos_y + " ball_width=" + ball_width);
		if ((x >= pos_x && x <= pos_x + ball_width) && (y >= pos_y && y <= pos_y + ball_width))
			return true;
		else
			return false;
	}

	public void flip_horz_direction() {
		delta_x *= -1;
		pos_x += delta_x;
		center_x = pos_x + radius;
	}

	public void flip_vert_direction() {
		delta_y *= -1;
		pos_y += delta_y;
		center_y = pos_y + radius;
	}

	public void set_delta(int delta_x_in, int delta_y_in) {
		delta_x = delta_x_in;
		delta_y = delta_y_in;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

}
